package com.houger.ddd.seckill.infra.datamanager.impl;

import com.houger.ddd.datamanager.entity.InventoryId;
import com.houger.ddd.datamanager.manager.aop.Inventory;
import com.houger.ddd.datamanager.operator.InventoryOperator;
import com.houger.ddd.datamanager.utils.InvLimitTypeEnum;
import com.houger.ddd.datamanager.utils.OperatorResult;
import com.houger.ddd.seckill.infra.entity.ActivityRewardItem;
import com.houger.ddd.seckill.repository.ActivityRewardItemRepository;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;


/**
 * @author Java架构师之旅
 * @date 2022年05月28日 15:05
 */
@Component
@Inventory("item-manager")
public class ItemManagerOperator implements InventoryOperator {
    @Resource
    private ActivityRewardItemRepository repository;
    @Override
    public long getTotal(final InventoryId id) {
        final ActivityRewardItem item = this.repository.findById(id.getId()).orElseThrow();
        final Integer invLimitType = item.getInvLimitType();
        if(invLimitType.equals(InvLimitTypeEnum.NO_LIMIT.getValue())){
            return Long.MAX_VALUE;
        }
        return item.getInvTotal().longValue();
    }

    @Override
    public long getUsed(final InventoryId id) {
        return 0;
    }

    @Override
    public OperatorResult deduct(final InventoryId id, final long cnt) {
        return null;
    }

    @Override
    public OperatorResult release(final InventoryId id, final long cnt) {
        return null;
    }

    @Override
    public boolean flushUsed(final InventoryId id, final long cnt) {
        return false;
    }
}
